package com.proactive.webapp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.proactive.webapp.dao.MenuDAO;
import com.proactive.webapp.entity.CourseCategory1;
import com.proactive.webapp.entity.Menu;

public class MenuDAOImpl implements MenuDAO{
	private DataSource dataSource;
	private Connection conn = null ;
	private ResultSet rs = null ;
	private PreparedStatement smt = null ;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public List<Menu> getList() {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM menu WHERE menuStatus = 1 ORDER BY menuSort ";
		List<Menu> menuList = new ArrayList<Menu>();
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			rs = smt.executeQuery();
			while(rs.next()){
				Menu menu = new Menu();
				menu.setId(rs.getInt("menuID"));
				menu.setName(rs.getString("menuName"));
				menu.setIcon(rs.getString("menuIcon"));
				menu.setUrl(rs.getString("menuUrl"));
				menuList.add(menu);
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return menuList;
	}

}
